<?php
namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\common\model\MyCar as MyCarModel;
use think\Db;
use think\Log;

class MyCar extends Command
{
    protected function configure()
    {
        $this->setName('myCar')->setDescription('expire myCar handle 过期用户座驾处理');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }

    /**
     * 过期用户座驾处理  每分钟执行一次
     */
    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo '任务正在执行中<br />';return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }

        $map['status'] = 1;
        $map['end_time'] = ['< time',time()];
        MyCarModel::where($map)->chunk(1000,function ($items){
            foreach ($items as $k => $v){
                Db::startTrans();
                try{
                    $v->update(['status' =>0], ['my_id' => $v['my_id']]);
                    Db::commit();
                }catch (\Exception $e){
                    Log::error($e->getMessage());
                    Db::rollback();
                }
            }
        });

        cache('command'.__CLASS__,null);
    }
}